const http = require('http');
const axios = require('axios');
const fs = require('fs');

function HttpSendPictures(path) { 
    fs.readFile(path, (err, data) => {
        if (err) throw err;
        console.log('data:', data.toString('base64'));
        HttpSendData(data.toString('base64'));
    });
}

function HttpSendData(data) {
    axios.post('http://localhost:4000/infer', {
     image: 'data:image/png;base64,'+data
    })
    .then(response => {
        console.log('response:',response.data.inference_results);
        Savepictures(response.data);
     })
    .catch(error => {
        console.log(error);
     });

}

function Savepictures(data) {
    let base64Data = data.result.split(';base64,')[1];
    // 创建Buffer对象
    let imageBuffer = Buffer.from(base64Data, 'base64');
    // 将Buffer写入文件
    fs.writeFile('src/image/output.png', imageBuffer, (err) => {
        if (err) {
            console.error('写入图片文件时出错', err);
        } else {
            console.log('图片已成功保存为 output.png');
        }
    });
}


// module.exports = {
//     HttpSendData: HttpSendData,
//     HttpSendPictures: HttpSendPictures
// }

//HttpSendPictures('src/image/test1.jpg')

let path = 'src/image/logo.png';
HttpSendPictures(path);
 

 
// const http = require('http');
// const axios = require('axios');
// axios.post('http://192.168.67.147:5000/infer', {
//      image: ''
//     })
//     .then(response => {
//         console.log("response:",response.data);
//      })
//     .catch(error => {
//         console.log("error:",error);
//         console.log();
//      });
